﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.BusinessObjects;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.DataAccess
{
    public class ContentItemProvider : ObservableCollection<UIElement>
    {
        #region Variables
        private DependencyObject Owner = null;
        #endregion

        #region Constructor
        public ContentItemProvider(DependencyObject Owner)
        {
            this.Owner = Owner;
        }
        #endregion

        #region Methods
        #region GetContent
        public ObservableCollection<UIElement> GetContent()
        {
            base.Clear();            
            ucContent _ContentOwner = (Owner as ucContent);
            int TimeSlotsPerHour = 60 / (int)_ContentOwner.TimeSlotsPerHour;
            int TimeSlotsPerDay = TimeSlotsPerHour * 24;
            
            switch (_ContentOwner.CurrentDateView)
            {
                case Enumerations.DateView.Day:
                    int CurrentMinutes = 0;

                    for (int i = 0; i < TimeSlotsPerDay; i++)
                    {
                        this.Add(CreateDayView(_ContentOwner, CurrentMinutes));
                        CurrentMinutes += (int)_ContentOwner.TimeSlotsPerHour;                       
                    }
                    break;
                case Enumerations.DateView.WorkWeek:
                case Enumerations.DateView.FullWeek:
                    int DayOffSet = 0;

                    if (_ContentOwner.CurrentDateView == Enumerations.DateView.WorkWeek)
                        DayOffSet = 5;
                    else if (_ContentOwner.CurrentDateView == Enumerations.DateView.FullWeek)
                        DayOffSet = 7;

                    DateTime _MondaysDate = _ContentOwner.CurrentDate.AddDays((int)System.DayOfWeek.Monday - (int)_ContentOwner.CurrentDate.DayOfWeek);

                    for (int i = 0; i < DayOffSet; i++)
                    {
                        double Width = (_ContentOwner.ActualWidth - 55 - 19) / DayOffSet;
                        StackPanel sp = new StackPanel();
                        Border brd = new Border();

                        brd.Width = Width;

                        DateTime dt = _MondaysDate.AddDays(i);
                        CurrentMinutes = 0;

                        for (int j = 0; j < TimeSlotsPerDay; j++)
                        {
                            sp.Children.Add(CreateDayView(_ContentOwner, CurrentMinutes));

                            CurrentMinutes += (int)_ContentOwner.TimeSlotsPerHour;

                            if (dt.Date == DateTime.Now.Date)
                            {
                                brd.BorderBrush = _ContentOwner.WeekCurrentDateBorderBrush;
                                brd.BorderThickness = _ContentOwner.WeekCurrentDateBorderThickness;
                            }                            
                        }

                        brd.Child = sp;
                        this.Add(brd);
                    }
                    break;
                case Enumerations.DateView.Month:
                    DateTime tempDate = new DateTime(_ContentOwner.CurrentDate.Year, _ContentOwner.CurrentDate.Month, 1);
                    System.DayOfWeek _FirstDayOfMonth = tempDate.DayOfWeek;
                    int _DayOffSet = 0;
                    double _Height = (_ContentOwner.ActualWidth - 50) / 7;

                    if (_FirstDayOfMonth != System.DayOfWeek.Sunday)
                        _DayOffSet = (int)_FirstDayOfMonth;

                    for (int i = 0; i < _DayOffSet; i++)
                    {
                        Grid grd = new Grid();
                        grd.Height = _Height;
                        grd.Width = _Height;
                        this.Add(grd);
                    }

                    for (int _DayValue = 1; _DayValue <= DateTime.DaysInMonth(_ContentOwner.CurrentDate.Year, _ContentOwner.CurrentDate.Month); _DayValue++)
                    {
                        tempDate = new DateTime(_ContentOwner.CurrentDate.Year, _ContentOwner.CurrentDate.Month, _DayValue);
                        this.Add(CreateMonthView(_ContentOwner, tempDate, _DayValue, _Height));
                    }
                    break;
                case Enumerations.DateView.Year:
                    int Year = _ContentOwner.CurrentDate.Year;

                    for (int i = 1; i <= 12; i++)
                    {
                        WrapPanel wp = new WrapPanel();
                        Border brd = new Border();
                        StackPanel sp = new StackPanel();
                        TextBlock tb = new TextBlock();

                        DateTime tempYearDate = new DateTime(_ContentOwner.CurrentDate.Year, i, 1);
                        System.DayOfWeek _YearFirstDayOfMonth = tempYearDate.DayOfWeek;
                        int _YearDayOffSet = 0;
                        double _YearHeight = (_ContentOwner.ActualWidth - 50) / 7;

                        tb.HorizontalAlignment = HorizontalAlignment.Center;
                        tb.Text = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.LongMonth, tempYearDate);
                        tb.Margin = new Thickness(0, 5, 0, 5);

                        brd.Margin = new Thickness(19, 10, 0, 10);
                        sp.Orientation = Orientation.Vertical;

                        sp.Children.Add(tb);
                        
                        brd.Width = _ContentOwner.ActualWidth;

                        ucMonthContent _Month = new ucMonthContent(_ContentOwner);

                        if (_YearFirstDayOfMonth != System.DayOfWeek.Sunday)
                            _YearDayOffSet = (int)_YearFirstDayOfMonth;

                        for (int j = 0; j < _YearDayOffSet; j++)
                        {
                            Grid grd = new Grid();
                            grd.Height = _YearHeight;
                            grd.Width = _YearHeight;
                            wp.Children.Add(grd);
                        }                        

                        for (int _DayValue = 1; _DayValue <= DateTime.DaysInMonth(_ContentOwner.CurrentDate.Year, i); _DayValue++)
                        {
                            tempYearDate = new DateTime(_ContentOwner.CurrentDate.Year, i, _DayValue);
                            wp.Children.Add(CreateMonthView(_ContentOwner, tempYearDate, _DayValue, _YearHeight));
                        }

                        sp.Children.Add(wp);
                        brd.Child = sp;
                        this.Add(brd);
                    }                    
                    break;
            }

            return this;
        }
        #endregion

        #region CreateDayView
        private ucDayContentItem CreateDayView(ucContent _ContentOwner, int CurrentMinutes)
        {
            ucDayContentItem _ContentItem = new ucDayContentItem()
            {
                BorderBrush = _ContentOwner.DayContentBorderBrush,
                BorderThickness = _ContentOwner.DayContentBorderThickness,
                Height = _ContentOwner.ContentHeight,
                DayContentForeground = _ContentOwner.DayContentForeground,
                DayContentHoverBackground = _ContentOwner.DayContentHoverBackground,
                DayContentHoverBorderBrush = _ContentOwner.DayContentHoverBorderBrush,
                DayContentHoverBorderThickness = _ContentOwner.DayContentHoverBorderThickness
            };

            TimeSpan CurrentHour = new TimeSpan(0, CurrentMinutes, 0);

            if ((CurrentHour.Hours < 8 || CurrentHour.Hours > 17) || _ContentOwner.CurrentDate.DayOfWeek == System.DayOfWeek.Saturday || _ContentOwner.CurrentDate.DayOfWeek == System.DayOfWeek.Sunday)
                _ContentItem.Background = _ContentOwner.DayContentNonWorkHoursBackground;
            else
                _ContentItem.Background = _ContentOwner.DayContentWorkHoursBackground;

            return _ContentItem;
        }
        #endregion

        #region CreateMonthView
        private ucMonthContentItem CreateMonthView(ucContent _ContentOwner, DateTime tempDate, int _DayValue, double _Height)
        {
            ucMonthContentItem _MonthContentItem = new ucMonthContentItem()
            {
                BorderBrush = _ContentOwner.MonthContentBorderBrush,
                BorderThickness = _ContentOwner.MonthContentBorderThickness,
                Width = _Height,
                Height = _Height,
                CurrentDate = tempDate
            };

            if (_ContentOwner.Appointments != null && _ContentOwner.Appointments.Count > 0)
            {
                foreach (Appointment app in _ContentOwner.Appointments)
                {
                    if (app.StartDate.Value.Date == tempDate.Date)
                    {
                        Border brd = new Border();
                        brd.BorderBrush = new SolidColorBrush(Colors.Green);
                        brd.BorderThickness = new Thickness(1);

                        TextBlock txtBlk = new TextBlock();
                        txtBlk.Text = app.Desc;
                        txtBlk.Foreground = new SolidColorBrush(Colors.White);

                        if (app.Value != null)
                        {
                            if (app.Value.Color != null)
                                brd.Background = app.Value.Color;
                            else
                                brd.Background = new SolidColorBrush(Colors.Green);
                        }
                        else
                            brd.Background = new SolidColorBrush(Colors.Green);

                        brd.Child = txtBlk;

                        _MonthContentItem.DayAppointmentList.Add(brd);
                    }
                }
            }

            if (tempDate.Date == DateTime.Now.Date)
            {
                _MonthContentItem.MonthContentHeaderBackground = _ContentOwner.MonthCurrentDateHeaderBackground;
                _MonthContentItem.MonthContentHeaderBorderBrush = _ContentOwner.MonthCurrentDateHeaderBorderBrush;
                _MonthContentItem.MonthContentHeaderBorderThickness = _ContentOwner.MonthCurrentDateHeaderBorderThickness;
                _MonthContentItem.MonthContentHeaderForeground = _ContentOwner.MonthCurrentDateHeaderForeground;
                _MonthContentItem.MonthContentBackground = _ContentOwner.MonthCurrentDateBackground;
                _MonthContentItem.MonthContentBorderBrush = _ContentOwner.MonthCurrentDateBorderBrush;
                _MonthContentItem.MonthContentBorderThickness = _ContentOwner.MonthCurrentDateBorderThickness;
            }
            else
            {
                _MonthContentItem.MonthContentHeaderBackground = _ContentOwner.MonthContentHeaderBackground;
                _MonthContentItem.MonthContentHeaderBorderBrush = _ContentOwner.MonthContentHeaderBorderBrush;
                _MonthContentItem.MonthContentHeaderBorderThickness = _ContentOwner.MonthContentHeaderBorderThickness;
                _MonthContentItem.MonthContentHeaderForeground = _ContentOwner.MonthContentHeaderForeground;
                _MonthContentItem.MonthContentBackground = _ContentOwner.MonthContentBackground;
                _MonthContentItem.MonthContentBorderBrush = _ContentOwner.MonthContentBorderBrush;
                _MonthContentItem.MonthContentBorderThickness = _ContentOwner.MonthContentBorderThickness;
            }

            if (_DayValue == 1)
                _MonthContentItem.DisplayText = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.ShortMonth, tempDate) + " " + _DayValue.ToString();
            else
                _MonthContentItem.DisplayText = _DayValue.ToString();

            return _MonthContentItem;
        }
        #endregion
        #endregion
    }
}
